/**
  * @file MTRT_SNInfoAPI.c
  * @date 2020骞�9鏈�1鏃�
  * @author Thinkpad
  * @copyright ModelingTech
  * @brief
  * @details
  * @version V1.0.0
  */
// #include <MTRT_BaremetalAPI.h>
#include "MTRT_SNInfoAPI.h"
#include "stdio.h"
#include "string.h"



SNLineInfo SNLineInfo_HILInit(){
	SNLineInfo SNLineInfo_HIL;
	SNLineInfo_HIL.VersionLen=3;
	return SNLineInfo_HIL;
}

//__uint64_t GetRCPDNA(int TargetType){
//	SharedReg *SharedReg_SN;
//	__uint64_t FPGA_DNA;
//	LoadBitFile(TargetType,RCPTestIO,StarSimRCP);
////	Baremetal_bit_reset(1,0);//0:RCP_1050_Run  1:RCP_1050_TestIO 2:HIL_6020_Run 3:HIL_6020_TestIO
//	usleep(100000);
//	Baremetal_elf_Start(5);//0:RCP_1050_Run  1:RCP_1050_TestIO 2:HIL_6020_Run 3:HIL_6020_TestIO 4.RCP Activation 5.HIL Activation
//	usleep(100000);
//	SharedReg_SN=SHM_RPC_Init();//RCPRT_BaremetalAPI
//	SharedReg_SN->BareMetalFunctionSelect=2;
//	usleep(100000);
//	printf("SharedReg_SN->DNA_High=%u\n",SharedReg_SN->DNA_High);
//	printf("SharedReg_SN->DNA_Low=%u\n",SharedReg_SN->DNA_Low);
//	FPGA_DNA = SharedReg_SN->DNA_High;
//	FPGA_DNA = (FPGA_DNA <<32);
//	FPGA_DNA |= SharedReg_SN->DNA_Low;
//	FILE *plot_fp = fopen("./home/RCPDNA.ini","w+");
//	fprintf(plot_fp, "%llu", FPGA_DNA);
////	fprintf(plot_fp, "%X\r\n", SharedReg_SN->DNA_Low);
//	fclose(plot_fp);
//	printf("FPGA_DNA=%llu\n",FPGA_DNA);
//	Baremetal_elf_Stop();
//	return FPGA_DNA;
//}
void CheckActivate(){

}

static char buff[1024];
void MTReadBinaryFile(char* iniPath){
	FILE *fp = NULL;
	int ch;
	fp = fopen(iniPath, "rb");
	if (!fp)
	{
		printf("Error in reading file. Abort.\n");
	}else{
		fread(buff,sizeof(buff),1,fp); // read 255 bytes to our buffer
	}
	fclose(fp);
	return;
}

SNLineInfo MTSNDatTrans(char* revbuf)
{
	SNLineInfo SNLineInfoData;
	__uint32_t ByteIndex=0, ByteLength,ArraySize;

	ByteLength=4;//u32 char Size
	memcpy(&ArraySize,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;
	printf("SNLineInfo char size: %d\n", ArraySize);

	ByteLength=4;//u32 SNLineInfoData ArraySize
	memcpy(&ArraySize,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;

	ByteLength=4;//u32 VersionLen
	memcpy(&ArraySize,&revbuf[ByteIndex],ByteLength);
	SNLineInfoData.VersionLen=ArraySize;
	ByteIndex = ByteIndex + ByteLength;

	ByteLength=ArraySize*sizeof(char);
	SNLineInfoData.Version=(char *)calloc(ByteLength,sizeof(char)+1);
	memcpy(SNLineInfoData.Version,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;
	printf("SNLineInfoData.Version=%s\n",SNLineInfoData.Version);

	ByteLength=4;//u32 SNLen
	memcpy(&ArraySize,&revbuf[ByteIndex],ByteLength);
	SNLineInfoData.SNLen=ArraySize;
	ByteIndex = ByteIndex + ByteLength;

	ByteLength=ArraySize*sizeof(char);
	SNLineInfoData.SN=(char *)calloc(ByteLength,sizeof(char)+1);
	memcpy(SNLineInfoData.SN,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;
	printf("SNLineInfoData.SN=%s\n",SNLineInfoData.SN);

	ByteLength=4;//u32 TargetLicInfoSize
	memcpy(&ArraySize,&revbuf[ByteIndex],ByteLength);
	SNLineInfoData.TargetLicInfoSize=ArraySize;
	ByteIndex = ByteIndex + ByteLength;

	ByteLength=ArraySize*sizeof(TargetLicInfo);
	SNLineInfoData.TargetLicInfoArray=(TargetLicInfo *)calloc(ByteLength,sizeof(TargetLicInfo));
	if(ArraySize)
	{
		for(int i=0;i<ArraySize;i++)
		{
			ByteLength = 4;//int PureTopo_Row
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].PureTopo_Row,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//int PureTopo_Cloumn
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].PureTopo_Cloumn,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//int WithMachine_Row
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].WithMachine_Row,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//int WithMachine_Cloumn
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].WithMachine_Cloumn,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//__uint32_t SupportMachineSize
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].SupportMachineSize,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength=SNLineInfoData.TargetLicInfoArray[i].SupportMachineSize*sizeof(__uint16_t);//__uint16_t *
			SNLineInfoData.TargetLicInfoArray[i].SupportMachine=(__uint16_t *)calloc(ByteLength,sizeof(__uint16_t));
			memcpy(SNLineInfoData.TargetLicInfoArray[i].SupportMachine,&revbuf[ByteIndex],ByteLength);
//			for(int j=0;j<SNLineInfoData.TargetLicInfoArray[i].SupportMachineSize;j++)
//				printf("SNLineInfoData.TargetLicInfoArray[%d].SupportMachine[%d]=%d\n",i,j,SNLineInfoData.TargetLicInfoArray[i].SupportMachine[j]);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 2;//__uint16_t Status
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].Status,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//int SolverID
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].SolverID,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//int EndDate
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].EndDate,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//int LastDate
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].LastDate,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;

			ByteLength = 4;//int BitfileType
			memcpy(&SNLineInfoData.TargetLicInfoArray[i].BitfileType,&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;
		}
	}
	ByteLength=8;//__uint64_t FPGADNA_6020
	memcpy(&SNLineInfoData.FPGADNA,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;

	ByteLength=8;//__uint64_t FPGADNA_6020
	memcpy(&SNLineInfoData.FPGASN,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;

	ByteLength=2;//__uint16_t ProductID
	memcpy(&SNLineInfoData.ProductID,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;

	ByteLength=2;//__uint16_t TargetType
	memcpy(&SNLineInfoData.TargetType,&revbuf[ByteIndex],ByteLength);
	ByteIndex = ByteIndex + ByteLength;

	return SNLineInfoData;
}

// 读取license文件
SNLineInfo MTReadSNDat(char* iniPath)
{
	MTReadBinaryFile(iniPath);
	SNLineInfo SNLineInfoData=MTSNDatTrans(buff);
	return SNLineInfoData;
}
/****
 * 20201203 bug fix zhaowei
 * **/
H2T_GetSN H2T_GetSNTrans(char *revbuf,__uint32_t *ByteIndex_p)
{
	H2T_GetSN H2T_GetSN_Data;
	__uint32_t ByteIndex, ByteLength, ArraySize = 0;
	ByteIndex=*ByteIndex_p;
	ByteLength = 4;//Type
	memcpy(&ArraySize,&revbuf[ByteIndex],ByteLength);
	H2T_GetSN_Data.SlotNumberSize=ArraySize;
	ByteIndex = ByteIndex + ByteLength;

	if(ArraySize<10){
		ByteLength=ArraySize*sizeof(int);
		H2T_GetSN_Data.SlotNumber = (int*)calloc(ByteLength,sizeof(char));
		for(int i=0;i<ArraySize;i++)
		{
			ByteLength = 4;//int
			memcpy(&H2T_GetSN_Data.SlotNumber[i],&revbuf[ByteIndex],ByteLength);
			ByteIndex = ByteIndex + ByteLength;
		}
		ByteLength = 8;//u64
		memcpy(&H2T_GetSN_Data.Version,&revbuf[ByteIndex],ByteLength);
		ByteIndex = ByteIndex + ByteLength;
	}
	*ByteIndex_p=ByteIndex;
    return H2T_GetSN_Data;
}

/****
 * 20201203 bug fix zhaowei
 * **/
